<?php
define('LIBBBCODE','1.00');
/************************************************************************/
/* FrameWork  test preview                                              */
/* ==================================================================== */
/*                                                                      */
/* This program is free software. You can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 2 of the License.       */
/************************************************************************/
//libBBcode.inc
//-------------------------------------------------------------------------------------------------------------------------

//bb2html: trasforma i bbcode in codice html
function bb2html($string){
    $string = str_replace("\n", "<br />", $string);
    $string = str_replace("\r", "", $string);
    $string = str_replace("[b]", "<b>", $string);
    $string = str_replace("[/b]", "</b>", $string);
    $string = str_replace("[i]", "<i>", $string);
    $string = str_replace("[/i]", "</i>", $string);
    $string = str_replace("[quote]", "<blockquote><hr noshade /><i>", $string);
    $string = str_replace("[/quote]", "</i><hr noshade /></blockquote>", $string);
    $string = str_replace("[code]", "<blockquote><pre>", $string);
    $string = str_replace("[/code]", "</pre></blockquote>", $string);
    $string = str_replace("[img]", "<br><img src=\"", $string);
    $string = str_replace("[/img]", "\" alt=\"uploaded_image\"><br>", $string);

    $string = str_replace("[red]", "<font color=#ff0000>", $string);
    $string = str_replace("[green]", "<font color=#00ff00>", $string);
    $string = str_replace("[blue]", "<font color=#0000ff>", $string);
    $string = str_replace("[pink]", "<font color=#ff00ff>", $string);
    $string = str_replace("[yellow]", "<font color=#ffff00>", $string);
    $string = str_replace("[cyan]", "<font color=#00ffff>", $string);
    $string = str_replace("[/red]", "</font><!-- red -->", $string);
    $string = str_replace("[/blue]", "</font><!-- blue -->", $string);
    $string = str_replace("[/green]", "</font><!-- green -->", $string);
    $string = str_replace("[/pink]", "</font><!-- pink -->", $string);
    $string = str_replace("[/yellow]", "</font><!-- yellow -->", $string);
    $string = str_replace("[/cyan]", "</font><!-- cyan -->", $string);

    #sostituisce url
    //*
    $items = explode("[/url]",$string);
    for ($i = 0; $i < count($items); $i++) {
	$url="";
	if(stristr($items[$i],"[url]")){
	    #print "ITEM$i--$items[$i]--<br>";
	    $url = ereg_replace(".*\[url\]", "", $items[$i]);
	    $url = ereg_replace("\[/url\].*", "", $url);
	    if ($url != "") {
		if (stristr($url, "http://") == FALSE) {
		    $nuovourl="<a target=\"new\" href=\"http://$url\">$url</a>";
		}
		else {
		    $nuovourl="<a target=\"new\" href=\"$url\">$url</a>";
		}
		$string=str_replace("[url]".$url."[/url]", $nuovourl, $string);
	    }
	}
    }
    //*/

    #sostituisce wikipedia
    //*
    $items = explode("[/wikipedia]",$string);
    for ($i = 0; $i < count($items); $i++) {
	$wp="";
	if(stristr($items[$i],"[wikipedia]")){
	    $lang=lang();

	    #print "ITEM$i--$items[$i]--<br>";
	    $wp = ereg_replace(".*\[wikipedia\]", "", $items[$i]);
	    $wp = ereg_replace("\[/wikipedia\].*", "", $wp);
	    if ($wp != "") {
		$nuovowp="<a target=\"new\" href=\"http://$lang.wikipedia.org/wiki/$wp\">$wp</a>";

		$string=str_replace("[wikipedia]".$wp."[/wikipedia]", $nuovowp, $string);
	    }
	}
    }
    //*/

    return $string;
}
//-------------------------------------------------------------------------------------------------------------------------

//html2bb: trasforma il codice html in bbcode dove possibile
function html2bb($string){
    $string = str_replace("</font><!-- cyan -->","[/cyan]", $string);
    $string = str_replace("</font><!-- yellow -->","[/yellow]", $string);
    $string = str_replace("</font><!-- pink -->","[/pink]", $string);
    $string = str_replace("</font><!-- green -->","[/green]", $string);
    $string = str_replace("</font><!-- blue -->","[/blue]", $string);
    $string = str_replace("</font><!-- red -->","[/red]", $string);
    $string = str_replace("<font color=#00ffff>","[cyan]", $string);
    $string = str_replace("<font color=#ffff00>","[yellow]", $string);
    $string = str_replace("<font color=#ff00ff>","[pink]", $string);
    $string = str_replace("<font color=#0000ff>","[blue]", $string);
    $string = str_replace("<font color=#00ff00>","[green]", $string);
    $string = str_replace("<font color=#ff0000>","[red]", $string);

    $string = str_replace("\" alt=\"uploaded_image\"><br>","[/img]", $string);
    $string = str_replace("<br><img src=\"","[img]", $string);
    $string = str_replace("</pre></blockquote>","[/code]", $string);
    $string = str_replace("<blockquote><pre>","[code]", $string);
    $string = str_replace("</i><hr noshade></blockquote>","[/quote]", $string);
    $string = str_replace( "<blockquote><hr noshade /><i>","[quote]", $string);
    $string = str_replace("</i>","[/i]",  $string);
    $string = str_replace("<i>","[i]",  $string);
    $string = str_replace("</b>","[/b]", $string);
    $string = str_replace("<b>", "[b]", $string);
    $string = str_replace("","\r",  $string);
    $string = str_replace("<br />","\n", $string);

return $string;
}
//-------------------------------------------------------------------------------------------------------------------------
function creatoolbar($textarea){
?>
    <a href="javascript:<?=$textarea?>.value  =<?=$textarea?>.value.substring(0, <?=$textarea?>.selectionStart)+':)'+<?=$textarea?>.value.substring(<?=$textarea?>.selectionEnd, <?=$textarea?>.textLength);<?=$textarea?>.focus();"><img src="libs/libEmoticons/images/01.png" border="0" alt="Happy"/></a>
    <a href="javascript:<?=$textarea?>.value  =<?=$textarea?>.value.substring(0, <?=$textarea?>.selectionStart)+':('+<?=$textarea?>.value.substring(<?=$textarea?>.selectionEnd, <?=$textarea?>.textLength);<?=$textarea?>.focus();"><img src="libs/libEmoticons/images/02.png" border="0" alt="Triste" /></a>
    <a href="javascript:<?=$textarea?>.value  =<?=$textarea?>.value.substring(0, <?=$textarea?>.selectionStart)+':o'+<?=$textarea?>.value.substring(<?=$textarea?>.selectionEnd, <?=$textarea?>.textLength);<?=$textarea?>.focus();"><img src="libs/libEmoticons/images/03.png" border="0" alt="sorpresa" /></a>
    <a href="javascript:<?=$textarea?>.value  =<?=$textarea?>.value.substring(0, <?=$textarea?>.selectionStart)+':p'+<?=$textarea?>.value.substring(<?=$textarea?>.selectionEnd, <?=$textarea?>.textLength);<?=$textarea?>.focus();"><img src="libs/libEmoticons/images/04.png" border="0" alt="linguaccia" /></a>
    <a href="javascript:<?=$textarea?>.value  =<?=$textarea?>.value.substring(0, <?=$textarea?>.selectionStart)+':D'+<?=$textarea?>.value.substring(<?=$textarea?>.selectionEnd, <?=$textarea?>.textLength);<?=$textarea?>.focus();"><img src="libs/libEmoticons/images/05.png" border="0" alt="risata"/></a>
    <a href="javascript:<?=$textarea?>.value  =<?=$textarea?>.value.substring(0, <?=$textarea?>.selectionStart)+':!'+<?=$textarea?>.value.substring(<?=$textarea?>.selectionEnd, <?=$textarea?>.textLength);<?=$textarea?>.focus();"><img src="libs/libEmoticons/images/06.png" border="0" alt="indifferente" /></a>
    <a href="javascript:<?=$textarea?>.value  =<?=$textarea?>.value.substring(0, <?=$textarea?>.selectionStart)+':O'+<?=$textarea?>.value.substring(<?=$textarea?>.selectionEnd, <?=$textarea?>.textLength);<?=$textarea?>.focus();"><img src="libs/libEmoticons/images/07.png" border="0" alt="sbalordito" /></a>
    <a href="javascript:<?=$textarea?>.value  =<?=$textarea?>.value.substring(0, <?=$textarea?>.selectionStart)+'8)'+<?=$textarea?>.value.substring(<?=$textarea?>.selectionEnd, <?=$textarea?>.textLength);<?=$textarea?>.focus();"><img src="libs/libEmoticons/images/08.png" border="0" alt="fighetto" /></a>
    <a href="javascript:<?=$textarea?>.value  =<?=$textarea?>.value.substring(0, <?=$textarea?>.selectionStart)+';)'+<?=$textarea?>.value.substring(<?=$textarea?>.selectionEnd, <?=$textarea?>.textLength);<?=$textarea?>.focus();"><img src="libs/libEmoticons/images/09.png" border="0" alt="occhiolino"/></a>
    <a href="javascript:<?=$textarea?>.value  =<?=$textarea?>.value.substring(0, <?=$textarea?>.selectionStart)+'[b]'+<?=$textarea?>.value.substring(<?=$textarea?>.selectionStart, <?=$textarea?>.selectionEnd)+'[/b]'+<?=$textarea?>.value.substring(<?=$textarea?>.selectionEnd, <?=$textarea?>.textLength);<?=$textarea?>.focus();"><img src="libs/libBBcode/images/bold.png" border="0" alt="bold"/></a>
    <a href="javascript:<?=$textarea?>.value  =<?=$textarea?>.value.substring(0, <?=$textarea?>.selectionStart)+'[i]'+<?=$textarea?>.value.substring(<?=$textarea?>.selectionStart, <?=$textarea?>.selectionEnd)+'[/i]'+<?=$textarea?>.value.substring(<?=$textarea?>.selectionEnd, <?=$textarea?>.textLength);<?=$textarea?>.focus();"><img src="libs/libBBcode/images//italic.png" border="0" alt="italic" /></a>
    <a href="javascript:<?=$textarea?>.value  =<?=$textarea?>.value.substring(0, <?=$textarea?>.selectionStart)+'[quote]'+<?=$textarea?>.value.substring(<?=$textarea?>.selectionStart, <?=$textarea?>.selectionEnd)+'[/quote]'+<?=$textarea?>.value.substring(<?=$textarea?>.selectionEnd, <?=$textarea?>.textLength);<?=$textarea?>.focus();"><img src="libs/libBBcode/images/quote.png" border="0" alt="quote" /></a>
    <a href="javascript:<?=$textarea?>.value  =<?=$textarea?>.value.substring(0, <?=$textarea?>.selectionStart)+'[code]'+<?=$textarea?>.value.substring(<?=$textarea?>.selectionStart, <?=$textarea?>.selectionEnd)+'[/code]'+<?=$textarea?>.value.substring(<?=$textarea?>.selectionEnd, <?=$textarea?>.textLength);<?=$textarea?>.focus();"><img src="libs/libBBcode/images/code.png" border="0" alt="code" /></a>
    <a href="javascript:<?=$textarea?>.value  =<?=$textarea?>.value.substring(0, <?=$textarea?>.selectionStart)+'[img]'+<?=$textarea?>.value.substring(<?=$textarea?>.selectionStart, <?=$textarea?>.selectionEnd)+'[/img]'+<?=$textarea?>.value.substring(<?=$textarea?>.selectionEnd, <?=$textarea?>.textLength);<?=$textarea?>.focus();"><img src="libs/libBBcode/images/image.png" border="0" alt="image" /></a>
    <a href="javascript:<?=$textarea?>.value  =<?=$textarea?>.value.substring(0, <?=$textarea?>.selectionStart)+'[red]'+<?=$textarea?>.value.substring(<?=$textarea?>.selectionStart, <?=$textarea?>.selectionEnd)+'[/red]'+<?=$textarea?>.value.substring(<?=$textarea?>.selectionEnd, <?=$textarea?>.textLength);<?=$textarea?>.focus();"><img src="libs/libBBcode/images/red.png" border="0" alt="red" /></a>
    <a href="javascript:<?=$textarea?>.value  =<?=$textarea?>.value.substring(0, <?=$textarea?>.selectionStart)+'[green]'+<?=$textarea?>.value.substring(<?=$textarea?>.selectionStart, <?=$textarea?>.selectionEnd)+'[/green]'+<?=$textarea?>.value.substring(<?=$textarea?>.selectionEnd, <?=$textarea?>.textLength);<?=$textarea?>.focus();"><img src="libs/libBBcode/images/green.png" border="0" alt="green" /></a>
    <a href="javascript:<?=$textarea?>.value  =<?=$textarea?>.value.substring(0, <?=$textarea?>.selectionStart)+'[blue]'+<?=$textarea?>.value.substring(<?=$textarea?>.selectionStart, <?=$textarea?>.selectionEnd)+'[/blue]'+<?=$textarea?>.value.substring(<?=$textarea?>.selectionEnd, <?=$textarea?>.textLength);<?=$textarea?>.focus();"><img src="libs/libBBcode/images/blue.png" border="0" alt="blue"/></a>
    <a href="javascript:<?=$textarea?>.value  =<?=$textarea?>.value.substring(0, <?=$textarea?>.selectionStart)+'[pink]'+<?=$textarea?>.value.substring(<?=$textarea?>.selectionStart, <?=$textarea?>.selectionEnd)+'[/pink]'+<?=$textarea?>.value.substring(<?=$textarea?>.selectionEnd, <?=$textarea?>.textLength);<?=$textarea?>.focus();"><img src="libs/libBBcode/images/pink.png" border="0" alt="pink" /></a>
    <a href="javascript:<?=$textarea?>.value  =<?=$textarea?>.value.substring(0, <?=$textarea?>.selectionStart)+'[yellow]'+<?=$textarea?>.value.substring(<?=$textarea?>.selectionStart, <?=$textarea?>.selectionEnd)+'[/yellow]'+<?=$textarea?>.value.substring(<?=$textarea?>.selectionEnd, <?=$textarea?>.textLength);<?=$textarea?>.focus();"><img src="libs/libBBcode/images/yellow.png" border="0" alt="yellow" /></a>
    <a href="javascript:<?=$textarea?>.value  =<?=$textarea?>.value.substring(0, <?=$textarea?>.selectionStart)+'[cyan]'+<?=$textarea?>.value.substring(<?=$textarea?>.selectionStart, <?=$textarea?>.selectionEnd)+'[/cyan]'+<?=$textarea?>.value.substring(<?=$textarea?>.selectionEnd, <?=$textarea?>.textLength);<?=$textarea?>.focus();"><img src="libs/libBBcode/images/cyan.png" border="0" alt="cyan" /></a>
    <a href="javascript:<?=$textarea?>.value  =<?=$textarea?>.value.substring(0, <?=$textarea?>.selectionStart)+'[url]'+<?=$textarea?>.value.substring(<?=$textarea?>.selectionStart, <?=$textarea?>.selectionEnd)+'[/url]'+<?=$textarea?>.value.substring(<?=$textarea?>.selectionEnd, <?=$textarea?>.textLength);<?=$textarea?>.focus();"><img src="libs/libBBcode/images/url.png" border="0" alt="url" /></a>
    <a href="javascript:<?=$textarea?>.value  =<?=$textarea?>.value.substring(0, <?=$textarea?>.selectionStart)+'[wikipedia]'+<?=$textarea?>.value.substring(<?=$textarea?>.selectionStart, <?=$textarea?>.selectionEnd)+'[/wikipedia]'+<?=$textarea?>.value.substring(<?=$textarea?>.selectionEnd, <?=$textarea?>.textLength);<?=$textarea?>.focus();"><img src="libs/libBBcode/images/wikipedia.png" border="0" alt="url" /></a>

<?php
}
?>
